﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace client.utils
{
    public class ImageUtils
    {
        private static ConcurrentDictionary<string, List<Image>> imageCache = new ConcurrentDictionary<string, List<Image>>();

        public static List<Image> loadImagesByObjectType(int objectType) {

            string imageLocation = null;

            switch (objectType)
            {

                case 1:
                    {
                        // 豌豆射手
                        imageLocation = ".\\assets\\plant\\peashooter\\";
                        break;
                    };
                case 2: {
                        // 向日葵
                        imageLocation = ".\\assets\\plant\\sunflower\\";
                        break;
                    }
                default:
                    {
                        throw new ArgumentException($"不支持创建对象类型为{objectType}的对象");

                    }

            }

            return loadImageList(imageLocation);
        }

        private static List<Image> loadImageList(string imageLocation)
        {
            List<Image> result;
            if (imageCache.TryGetValue(imageLocation, out result))
            {

                Console.WriteLine(imageLocation + " 缓存命中");
                return result;
            }
            var dic = new DirectoryInfo(imageLocation);

            result = dic.GetFiles()
                            .Where(fileinfo => fileinfo.Name.EndsWith("png"))
                            .Select(fileInfo => Image.FromFile(fileInfo.FullName))
                            .Select(img =>
                            {

                                //var res = new Bitmap(img);
                                //res.MakeTransparent(Color.White);


                                return toTransparent(img);
                            })
                            .ToList();


            if (result.Count > 0)
            {

                imageCache.TryAdd(imageLocation, result);
                return result;
            }
            throw new ArgumentException($"{imageLocation} 下不能找到有效的图片");
        }

        private static Image toTransparent(Image originalImage)
        {

            // 转换为位图
            Bitmap originalBitmap = new Bitmap(originalImage);

            // 创建透明位图
            Bitmap transparentBitmap = new Bitmap(originalBitmap.Width, originalBitmap.Height);

            // 设置透明色（例如，将白色设置为透明）
            Color transparentColor = Color.White;

            // 遍历每个像素并设置透明色
            for (int x = 0; x < originalBitmap.Width; x++)
            {
                for (int y = 0; y < originalBitmap.Height; y++)
                {
                    Color pixelColor = originalBitmap.GetPixel(x, y);

                    // 如果像素颜色不是透明色，则将其复制到新的位图中
                    if (pixelColor != transparentColor)
                    {
                        transparentBitmap.SetPixel(x, y, pixelColor);
                    }
                    // 如果是透明色，则将新位图中的对应像素设置为透明
                    else
                    {
                        transparentBitmap.SetPixel(x, y, Color.Transparent);
                    }
                }
            }
            return transparentBitmap;

        }

        public static Image CombineImages(Image[] images)
        {



            var finalWidth = images.Max(img => img.Width);

            var finalHeight = images.Max(img => img.Height);

            var finalImg = new Bitmap(finalWidth, finalHeight);
            Graphics g = Graphics.FromImage(finalImg);
            g.Clear(SystemColors.AppWorkspace);

            //var width = finalWidth;
            //var height = finalHeight;
           
            foreach (Image img in images)
            {

                g.DrawImage(img, new Point(finalWidth/2, finalHeight/2));

                //img.Dispose();
            }
            g.Dispose();
            //finalImg.Save(finalImage, System.Drawing.Imaging.ImageFormat.Tiff);
            //finalImg.Dispose();
            return finalImg;
        }

    }
}
